from django.shortcuts import render, redirect
from django.http import HttpRequest, HttpResponse

from lists.models import Item, List
# Create your views here.

def lists_home(request):
	return render(request, 'lists/index.html')


def view_list(request, list_id):
	list_ = List.objects.get(id=list_id)
	items = Item.objects.filter(list=list_)
	return render(request, 'lists/list.html', {
		'list': list_, 'items': items,
	})


def new_list(request):
	list_ = List.objects.create()
	Item.objects.create(text=request.POST["item_text"], list=list_)
	return redirect('/lists/%d/' % (list_.id, ))


def add_item(request, list_id):
	list_ = List.objects.get(id=list_id)
	Item.objects.create(text=request.POST["item_text"], list=list_)
	return redirect('/lists/%d/' % (list_.id, ))
